def composite_trapezoidal_fixed(a, b, f, n):
    """使用给定的分段数 n 进行复化梯形积分计算。"""
    h = (b - a) / n
    sum_trapezoids = sum(f(a + k * h) for k in range(1, n))
    sum_trapezoids += 0.5 * (f(a) + f(b))
    sum_trapezoids *= h
    return sum_trapezoids

def composite_trapezoidal(a, b, f, epsilon=1e-5):
    """根据误差 epsilon 选择分段数，调用复化梯形积分。"""
    n = 1
    last_approximation = composite_trapezoidal_fixed(a, b, f, n)
    
    while True:
        # n 每轮倍增，等区间划分，并计算新的近似值
        n *= 2
        current_approximation = composite_trapezoidal_fixed(a, b, f, n)
        
        # 检查误差是否满足要求
        if abs(current_approximation - last_approximation) < epsilon:
            break
        
        # 更新结果
        last_approximation = current_approximation
    
    return current_approximation, n